$(function() {
	var url_modify = "/ag/dgm/modify_PlanType.action"; // 一些url
	var url_find = "/ag/dgm/find_PlanType.action";
	var url_add = "/ag/dgm/add_PlanType.action";
	var url_delete = "/ag/dgm/delete_PlanType.action"

		$("#dialog").dialog("destroy");
	var name = $("#name"), detail = $("#detail"), allFields = $( []).add(name)
			.add(detail), tips = $(".validateTips");

	function updateTips(t) {
		tips.text(t).addClass('ui-state-highlight');
		setTimeout(function() {
			tips.removeClass('ui-state-highlight', 1500);
		}, 500);
	}

	function checkLength(o, n, min, max) {

		var dtDealingType1 = $.trim(o.val());
		var cArr1 = dtDealingType1.match(/[^x00-xff]/ig);
		var the_length = dtDealingType1.length
				+ (cArr1 == null ? 0 : cArr1.length);
		if (the_length > max || the_length < min) {
			o.addClass('ui-state-error');
			updateTips(n + "长度必须在 " + min + " 和 " + max + "之间.");
			return false;
		} else {
			return true;
		}

	}

	function checkRegexp(o, regexp, n) {
		if (!(regexp.test(o.val()))) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}

	}
	/** **********************修改begin***************************** */
	$('.modify').click(function() {                        // 点修改
		var id_val = $.trim($(this).parent().siblings(".id_").text()); // 取兄弟即id的内容放到弹出form
		var name_val = $.trim($(this).parent().siblings(".name_")
				.text());
		var detail_val = $.trim($(this).parent().siblings(".detail_")
				.text());

		$("textarea[name='detail_modify']").val(detail_val);
		$("input[name='name_modify']").val(name_val);
		$("input[name='id_modify']").val(id_val);

		$('#dialog-form-modify').dialog('open');

	});
	
	
	$("#dialog-form-modify").dialog( { // 修改form的情况
				autoOpen : false,
				height : 300,
				width : 350,
				modal : true,
				buttons : {
					'提交' : function() {
						name = $("input[name='name_modify']");
						detail = $("textarea[name='detail_modify']");
						/* alert(detail.val()); */
						var bValid = true;
						/* $("button[type='button']").attr("disabled","disabled"); */
						allFields.removeClass('ui-state-error');
						bValid = bValid && checkLength(name, "名字", 1, 20);
						bValid = bValid && checkLength(detail, "描述", 0, 50);
						if (bValid) {
							$("#confirmUI_modify").dialog('open');
						}
					},
					'取消' : function() {
						$(this).dialog('close');
					}
				},
				close : function() {
					allFields.val('').removeClass('ui-state-error');
				}
			});

	$("#confirmUI_modify").dialog( { // 修改时的confirm
				autoOpen : false,
				height : 100,
				width : 350,
				modal : true,
				buttons : {
					'修改' : function() {
						$("button[name='修改']").attr("disabled", "disabled");
						allFields.removeClass('ui-state-error');
						/* 这里提交 */
						var Id = $("input[name='id_modify']").val();
						var Name = $("input[name='name_modify']").val();
						var Detail = $("textarea[name='detail_modify']").val();

						var req = {
							Id : Id,
							Name : Name,
							Detail : Detail
						};

						var url = url_modify;
						$.post(url, req, function(data) { // 传入到action
									result_alert(data);
								});
						/* 提交结束 */
					},
					'取消' : function() {
						$(this).dialog('close');
					}
				},
				close : function() {
					allFields.val('').removeClass('ui-state-error');
				}
			});

/** ***********************修改end ***************************** */
	/** **********删除begin******************* */
	$('button.delete').click(function() {
		$('#confirmUI_delete').dialog('open');
		var Id_val = $.trim($(this).parent().siblings(".id_").text()); // 取兄弟即id的内容放到弹出form
		
		$("input[name='id_delete']").val(Id_val);
	});
	
	
	$("#confirmUI_delete").dialog( { // 修改时的confirm
		autoOpen : false,
		height : 100,
		width : 350,
		modal : true,
		buttons : {
			'删除' : function() {
				$("button[name='删除']").attr("disabled", "disabled");
				allFields.removeClass('ui-state-error');
				/* 这里提交 */
				var Id = $("input[name='id_delete']").val();
				
				var req = {
					Id : Id

				};

				var url = url_delete;
				$.post(url, req, function(data) { // 传入到action
							result_alert(data);
						});
				/* 提交结束 */
			},
			'取消' : function() {
				$(this).dialog('close');
			}
		},
		close : function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	
	
	/** **********删除end******************* */
	
	/* *************返回成功与否begin ******************* */
	function result_alert(data) {
		$("div[name='alert']").text(data.result).dialog('open');
	}

	$("div[name='alert']").dialog( {
		autoOpen : false,
		height : 100,
		width : 350,
		modal : true,
		buttons : {
			'确定' : function() {
				allFields.removeClass('ui-state-error');
				location.href = url_find;
			}
		},
		close : function() {
			allFields.val('').removeClass('ui-state-error');
			location.href = url_find;
		}
	});

	/** **************** 返回成功与否end ***************** */
	/** **************** 增加begin ***************** */
	$('#create-user').click(function() {
		$('#dialog-form').dialog('open');

	});

	
	$("#dialog-form").dialog( {                 // 增加form的情况
				autoOpen : false,
				height : 300,
				width : 350,
				modal : true,
				buttons : {

					'保存' : function() {
						name = $("input[name='name_add']");
						detail = $("textarea[name='detail_add']");
						var bValid = true;

						allFields.removeClass('ui-state-error');
						bValid = bValid && checkLength(name, "名字", 1, 20);
						bValid = bValid && checkLength(detail, "描述", 0, 50);
						if (bValid)
							$("#confirmUI_add").dialog('open');
					},

					'取消' : function() {
						$(this).dialog('close');
					}
				},
				close : function() {
					allFields.val('').removeClass('ui-state-error');
				}
			});

	$("#confirmUI_add").dialog( {                // 增加confirm
				autoOpen : false,
				height : 100,
				width : 350,
				modal : true,
				buttons : {
					'增加' : function() {
						$("button[name='增加']").attr("disabled", "disabled");
						allFields.removeClass('ui-state-error');
						/* 这里提交 */
						/* var Id = $("input[name='id_add']").val(); */
						var Name = $("input[name='name_add']").val();
						var Detail = $("textarea[name='detail_add']").val();

						var req = {
							/* Id : Id, */
							Name : Name,
							Detail : Detail
						};

						var url = url_add;
						$.post(url, req, function(data) { // 传入到action
									result_alert(data);
								});
						/* 提交结束 */
					},
					'取消' : function() {
						$(this).dialog('close');
					}
				},
				close : function() {
					allFields.val('').removeClass('ui-state-error');
				}
			});





/*	$("tr.list").mouseover(function() {                                 //行跟着鼠标闪效果
		$(this).children().css("background-color", "#66FFFF");
	}).mouseout(function() {
		$(this).children().css("background-color", "white");
	});*/

});
